﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameObjects
{
    public class Sprite
    {
        Texture2D textureImage;
        protected Point frameSize;
        Point currentFrame;
        Point sheetSize;
        int collisionOffset;
        int timeSinceLastFrame = 0;
        int millisecondsPerFrame;
        const int defaultMillisecondsPerFrame = 16;
        //protected Vector2 speed;
        //protected Vector2 position;
        //public Vector2 direction;

        //public Rectangle collisionRect
        //{
        //    get
        //    {
        //        return new Rectangle(
        //            (int)position.X + collisionOffset,
        //            (int)position.Y + collisionOffset,
        //            frameSize.X - collisionOffset * 2,
        //            frameSize.Y - collisionOffset * 2);
        //    }
        //}

        public Sprite(Texture2D textureImage, Point frameSize,
            int collisionOffset, Point currentFrame, Point sheetSize)
            : this(textureImage, frameSize, collisionOffset, currentFrame,
            sheetSize, defaultMillisecondsPerFrame)
        {
        }

        public Sprite(Texture2D textureImage, Point frameSize,
            int collisionOffset, Point currentFrame, Point sheetSize,
            int millisecondsPerFrame)
        {
            this.textureImage = textureImage;
            //this.position = position;
            this.frameSize = frameSize;
            this.collisionOffset = collisionOffset;
            this.currentFrame = currentFrame;
            this.sheetSize = sheetSize;
            //this.speed = speed;
            this.millisecondsPerFrame = millisecondsPerFrame;
        }

        /// <summary>
        /// Позволяет игре запускать логику обновления мира,
        /// проверки столкновений, получения ввода и воспроизведения звуков.
        /// </summary>
        /// <param name="gameTime">Предоставляет моментальный снимок значений времени.</param>
        public void Update(GameTime gameTime)
        {
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > millisecondsPerFrame)
            {
                timeSinceLastFrame = 0;
                ++currentFrame.X;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.X = 0;
                    ++currentFrame.Y;
                    if (currentFrame.Y >= sheetSize.Y)
                        currentFrame.Y = 0;
                }
            }
        }

        /// <summary>
        /// Вызывается, когда игра отрисовывается.
        /// </summary>
        /// <param name="gameTime">Предоставляет моментальный снимок значений времени.</param>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Character character)
        {
            spriteBatch.Draw(textureImage,
                character.Position,
                new Rectangle(currentFrame.X * currentFrame.Y,
                    frameSize.X * frameSize.Y,
                    frameSize.X, frameSize.Y),
                    Color.White, 0, Vector2.Zero,
                    1f, SpriteEffects.None, 0);
        }
    }
}
